package com.qf.studentmanager.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.studentmanager.model.Exam;
import com.qf.studentmanager.model.RespBean;
import com.qf.studentmanager.model.RespPageBean;
import com.qf.studentmanager.service.ExamService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(urlPatterns = "/Exam")
public class ExamServlet extends HttpServlet {
    ExamService examService = new ExamService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        RespBean  respBean = null;
        String examName = request.getParameter("name");
        String date = request.getParameter("examDate");
        Date examDate = null;
        try {
             examDate = new SimpleDateFormat("yyyy-MM-dd").parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String examType = request.getParameter("examType");
        String gid = request.getParameter("gid");
        String cid = request.getParameter("cid");
        String crid = request.getParameter("crid");
        String remark = request.getParameter("remark");
        Exam exam = new Exam(examName,examDate,examType,Integer.parseInt(gid),Integer.parseInt(cid),Integer.parseInt(crid),remark);

        Integer i = examService.addExam(exam);
        if (i == 1){
            respBean = RespBean.ok("添加成功");
        }else if (i == -2){
            respBean = RespBean.ok("考试重复 添加失败");
        }else {
            respBean = RespBean.ok("添加失败");
        }

        String string = objectMapper.writeValueAsString(respBean);
        response.getWriter().write(string);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        String action = request.getParameter("action");

        if ("page".equals(action)) {
            request.getRequestDispatcher("WEB-INF/jsp/exam/examList.jsp").forward(request, response);
        } else if ("data".equals(action)) {
            String page = request.getParameter("page");
            String rows = request.getParameter("rows");
            String sort = request.getParameter("sort");
            String order = request.getParameter("order");
            String gid = request.getParameter("gid");
            String cid = request.getParameter("cid");

            //获取考试数据getall***(){}返回resppagebean
            RespPageBean respPageBean = examService.getAllExamByPage(page, rows, sort, order, gid, cid);
            String string = objectMapper.writeValueAsString(respPageBean);
            response.getWriter().write(string);
        }

    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        String  eid = request.getParameter("eid");
        RespBean respBean = null;

        Integer i = examService.deleteExamByEid(eid);
        if (i == 1){
            respBean = RespBean.ok("删除成功");
        }else {
            respBean = RespBean.ok("删除失败");
        }

        String string = objectMapper.writeValueAsString(respBean);
        response.getWriter().write(string);
    }
}
